package com.smalluz.ad.modules.common.util.json;
 


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

/**
 * @description: 转换null对象为空字符串
 */
public class JsonObjectMapper extends ObjectMapper {
    private static final long serialVersionUID = 1L;

    private final DateFormat ISO8601FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @SuppressWarnings("deprecation")
    public JsonObjectMapper(boolean isTrue) {
        super();
        // 空值处理为空串
        this.setDateFormat(ISO8601FORMAT);
        // 不为空的字段就会参与转换并且输出
        this.setSerializationInclusion(Include.NON_NULL);
        // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
        this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        // 该配置解决 Illegal unquoted character ((CTRL-CHAR, code 9)) 的问题
        this.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);

        SimpleModule simpleModule = new SimpleModule("JsonMapSerializer", Version.unknownVersion());

        // 对LocalDateTime类，提供统一的序列化方式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        simpleModule.addSerializer(new LocalDateTimeSerializer(dateTimeFormatter));
        this.registerModule(simpleModule);
        // 统一返回数据的输出风格
        this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        this.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        // 将驼峰属性转换成下划线格式
        if (isTrue) {
             this.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        }
    }

    public JsonObjectMapper() {
        super();
        // 空值处理为空串
        this.setDateFormat(ISO8601FORMAT);
        // 不为空的字段就会参与转换并且输出
        this.setSerializationInclusion(Include.NON_NULL);
        // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
        this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        // 该配置解决 Illegal unquoted character ((CTRL-CHAR, code 9)) 的问题
        this.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);

        SimpleModule simpleModule = new SimpleModule("JsonMapSerializer", Version.unknownVersion());

        // 对LocalDateTime类，提供统一的序列化方式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        simpleModule.addSerializer(new LocalDateTimeSerializer(dateTimeFormatter));
        this.registerModule(simpleModule);
        // 统一返回数据的输出风格
        this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        this.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }
}
